package com.gt.andr.webbrowser;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class BroswerActivity extends Activity {
	/** Called when the activity is first created. */
	WebView webview;
	Button goBtn;
	EditText urlTxt;
	String htmlString = "<html><body>a whole <b>bunch</b> of html stuff<table border=\"2\"><tr><td>Hello Web <i>View</i></td></tr></table><marquee>Hello marquee test</marquee></body></html>";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browser);
		initComponents();
//		loadMyHtml();

		// just a test
		String htmlTextStr = Html.fromHtml(htmlString).toString();
		System.out.println(htmlTextStr);
	}

	private void loadMyHtml() {
		webview.loadData(htmlString, "text/html", "utf-8");

	}

	private void initComponents() {
		goBtn = (Button) findViewById(R.id.goBtn);
		goBtn.setOnClickListener(new GoBtnListener());
		webview = (WebView) findViewById(R.id.webview);
		webview.getSettings().setJavaScriptEnabled(true);
		 webview.loadUrl("http://www.google.com");
		webview.setWebViewClient(new MyWebViewClient());
		urlTxt = (EditText) findViewById(R.id.urlTxt);
	}

	private class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}

	private class GoBtnListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			String url = urlTxt.getText().toString().trim();
			if (!url.equals("")) {
				webview.loadUrl(url);
				Log.i("", webview.getTitle());
			}
		}
	}
}